﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SMS.UserManager;
using SMS.ServiceManager;
using digit.Core;

public partial class Modules_ReturnProcess : WebControlBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!ClientState.IsAuthenticated)
        {
            Response.Redirect("~/Login.aspx?session=timeout");
        }

        this.lnkUpdate.Attributes.Add("onclick", "return confirm('Bạn đã chắc chắn xóa?');");
        this.lnkUpdateBottom.Attributes.Add("onclick", "return confirm('Bạn đã chắc chắn xóa?');");

        if (ClientState.MemberInfo.UserType != 0)
        {
            lnkUpdate.Enabled = false;
            lnkUpdateBottom.Enabled = false;
        }
        else
        {
            lnkUpdate.Enabled = true;
            lnkUpdateBottom.Enabled = true;
        }

        if (IsPostBack) return;

        ViewState["SortExpression"] = "MsgInID";
        ViewState["SortDirection"] = SortDirection.Descending;

        this.BindData();
    }

    protected void lnkUpdate_Click(object sender, EventArgs e)
    {
        int iUpdated = 0;

        //Search all checked rows to delete
        foreach (GridViewRow r in this.grvSms.Rows)
        {
            //If checkbox is not checked then quit
            HtmlInputCheckBox cbx = (HtmlInputCheckBox)r.Cells[0].Controls[1];
            if (!cbx.Checked) continue;

            //If checkbox is checked then delete
            if (MessageInManager.UpdateReturnProcess(Util.ToInteger(cbx.Value), this.ddlServiceID.SelectedValue, 0))
                iUpdated++;
        }

        //Display result of update process
        if (iUpdated == 0)
        {
            this.lblError.Text = "Không có bản ghi nào được xóa!";
            return;
        }

        this.lblError.Text = "Đã cập nhật " + iUpdated.ToString() + " bản ghi!";

        this.BindData();
    }

    protected void grvSms_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.grvSms.PageIndex = e.NewPageIndex;
        this.BindData();
    }

    protected void grvSms_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (ViewState["SortExpression"].ToString() == e.SortExpression)
        {
            ViewState["SortDirection"] = ((SortDirection)ViewState["SortDirection"] == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending);
        }
        else
        {
            ViewState["SortExpression"] = e.SortExpression;
            ViewState["SortDirection"] = SortDirection.Ascending;
        }
        BindData();
    }

    private void BindData()
    {
        try
        {
            string strServiceID = "";
            strServiceID = this.ddlServiceID.SelectedValue;

            DataTable dt = MessageInManager.SelectByServiceID(strServiceID);

            if (dt != null && dt.Rows.Count > 0)
            {
                DataView dv = dt.DefaultView;

                dv.Sort = Util.GetSortClause(ViewState["SortExpression"].ToString(), (SortDirection)ViewState["SortDirection"]);
                if (ViewState["SortExpression"].ToString() != "MsgInID")
                {
                    dv.Sort += ", " + Util.GetSortClause("MsgInID", SortDirection.Ascending);
                }

                this.grvSms.DataSource = dv;
                this.grvSms.DataBind();
                this.lblError.Text = "Tổng số <b>" + dt.Rows.Count + "</b> tin nhắn lỗi chờ cập nhật";
            }
            else
            {
                lblError.Text = "Hiện tại chưa có tin mới nào đến!";
            }
        }
        catch { }
    }
}
